package com.todod.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.mapper.PublicFileRecordsMapper;
import com.todod.model.PublicFileRecords;

@Service
@Transactional
public class PublicFileRecordsService {

	@Autowired
	private PublicFileRecordsMapper publicFileRecordsMapper;

	/**
	 * 
	 * @param _userId
	 * @param publicFileRecords
	 * @param value
	 * @param value2
	 */
	public void saveData(Long userId, Long fileId, String flag) {

		PublicFileRecords publicFileRecords = new PublicFileRecords();
		publicFileRecords.setId(SnowflakeIdGenerator.getId());
		publicFileRecords.setFileId(fileId);
		publicFileRecords.setUserId(userId);
		publicFileRecords.setFlag(flag);

		publicFileRecords.setCreatetimeTi(LocalDateTime.now());
		publicFileRecords.setCreateuserId(userId);
		publicFileRecordsMapper.insert(publicFileRecords);
	}

	/**
	 * 
	 * @param _userId
	 * @param projectInfo
	 * @param value
	 * @param value2
	 */
	public void editData(Long _userId, Long fileId, String date, String downloads, String views) {

		PublicFileRecords publicFileRecords1 = getEvaluateListByFileId(fileId, date);

		publicFileRecords1.setModifytimeTi(LocalDateTime.now());
		publicFileRecords1.setModifyuserId(_userId);
		publicFileRecordsMapper.updateById(publicFileRecords1);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public List<PublicFileRecords> getEvaluateListByFileId(Long fileId) {
		QueryWrapper<PublicFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_file_mid", fileId);

		List<PublicFileRecords> list = publicFileRecordsMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public PublicFileRecords getEvaluateListByFileId(Long fileId, String date) {
		QueryWrapper<PublicFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_file_mid", fileId);
		queryWrapper.eq("gfr_date", date);

		return publicFileRecordsMapper.selectOne(queryWrapper);
	}

	public boolean getEvaluateListByDate(Long fileId, String date) {
		QueryWrapper<PublicFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_file_mid", fileId);
		queryWrapper.eq("gfr_date", date);

		List<PublicFileRecords> list = publicFileRecordsMapper.selectList(queryWrapper);
		if (list.size() > 0) {
			return false;
		} else {
			return true;
		}
	}

	public List<PublicFileRecords> getSortByDate(String format) {
		QueryWrapper<PublicFileRecords> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("gfr_date", format);

		List<PublicFileRecords> list = publicFileRecordsMapper.selectList(queryWrapper);

		return list;
	}

}
